package com.breezelin.freshmarket.adpaters;

/**
 * Created by Breeze Lin
 * 1:15 2016/5/19
 * 1linyufeng1@gmail.com
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.breezelin.freshmarket.R;
import com.breezelin.freshmarket.entities.data.Product;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * 产品列表的适配器
 */
public class ProductsAdapter extends BaseAdapter {

	/**
	 * 上下文
	 */
	private Context context;
	/**
	 * 产品集合
	 */
	private List<Product> products;
	/**
	 * 列表监听
	 */
	private ProductsListener listener;

	public ProductsAdapter(Context context, List<Product> products, ProductsListener listener) {
		this.context = context;
		this.products = products;
		this.listener = listener;
	}

	@Override
	public int getCount() {
		int ret = 0;
		if (products != null) {
			ret = products.size();
		}
		return ret;
	}

	@Override
	public Object getItem(int position) {
		return products.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		Item item = convertView != null ? (Item) convertView.getTag() : new Item(
				LayoutInflater.from(context).inflate(R.layout.item_product, parent, false));
		item.setData(products.get(position));
		if (position == getCount() - 1) {
			listener.onPageEnd();
		}
		return item.getRootView();
	}

	/**
	 * 条目实体
	 */
	private class Item implements View.OnClickListener {

		/**
		 * 产品实体
		 */
		private Product product;
		/**
		 * 根视图
		 */
		private View rootView;
		/**
		 * 封面
		 */
		private ImageView coverImg;
		/**
		 * 标题
		 */
		private TextView nameTxt;
		/**
		 * 规格
		 */
		private TextView specTxt;
		/**
		 * 价格
		 */
		private TextView priceTxt;

		public Item(View rootView) {
			this.rootView = rootView;

			// 绑定视图
			coverImg = (ImageView) rootView.findViewById(R.id.cover);
			nameTxt = (TextView) rootView.findViewById(R.id.name);
			specTxt = (TextView) rootView.findViewById(R.id.spec);
			priceTxt = (TextView) rootView.findViewById(R.id.price);
			// 设置监听
			rootView.setOnClickListener(this);
			// 绑定实体和视图
			rootView.setTag(this);
		}

		/**
		 * @return 条目的根视图
		 */
		public View getRootView() {
			return rootView;
		}

		/**
		 * 设置当前条目
		 *
		 * @param product 当前条目的产品
		 */
		public void setData(Product product) {
			this.product = product;

			// 设置视图
			Picasso.with(context).load(product.getSmallPic()).into(coverImg);
			nameTxt.setText(product.getCommodityName());
			specTxt.setText(product.getSpec());
			String price = "$" + product.getCommodityPrice();
			priceTxt.setText(price);
		}

		@Override
		public void onClick(View v) {
			listener.onProductSelected(product);
		}
	}

	/**
	 * 产品列表监听
	 */
	public interface ProductsListener {

		/**
		 * 产品点击回调
		 *
		 * @param product 产品信息
		 */
		void onProductSelected(Product product);

		/**
		 * 列表底部回调
		 */
		void onPageEnd();
	}
}
